<?php

namespace app\admin\service\setting;

use tmcore\base\BaseService;
use tmcore\services\SysClearService;

/**
 * 系统操作服务类
 */
class SystemService extends BaseService
{

    /**
     * 系统环境信息
     * @return array
     */
    public static function getInfo() : array
    {
        $sys_info = com_get_config('all', 'env');

        $key_names = [
            'os' => '操作系统',
            'dns' => '服务器域名',
            'ip' => 'IP',
            'port' => '端口',
            'php_version' => 'php版本',
            'php_api' => 'php运行环境',
            'disk_free_space' => '服务器可用空间',
            'upload_max_filesize' => '文件上传限制',
            'max_execution_time' => '最大执行时间',
        ];

        $data = [];

        foreach ($sys_info as $key => $value) {
            if(array_key_exists($key, $key_names)) array_push($data, ['key' => $key, 'desc' => $key_names[$key], 'value' => $value]);
        }

        return $data;
    }

    /**
     * 系统消息列表
     * @param $params
     * @return array
     */
    public static function getMessageList($params) : array
    {
        $list = [];

        if($params['type'] == 1) {
            $list = [
                ['title' => '版本发布的通知', 'value' => '基于Vue3 + ElementPlus + Nuxt3 + uniapp + Thinkphp8 的综合解决方案', 'time' => date('Y-m-d H:i:s', time())],
                ['title' => '学习交流的通知', 'value' => 'QQ群号码 665452019，欢迎小伙伴入群学习交流探讨！', 'time' => date('Y-m-d H:i:s', time())],
            ];
        } elseif ($params['type'] == 2) {
            $list = [
                ['title' => 'BUG修复', 'value' => '还有1个待修复问题，请马上处理', 'time' => date('Y-m-d H:i:s', time())],
                ['title' => '功能开发', 'value' => '还有2个待上线功能，请马上处理', 'time' => date('Y-m-d H:i:s', time())],
                ['title' => '系统重构', 'value' => '还有3个待重构模块，请马上处理', 'time' => date('Y-m-d H:i:s', time())],
            ];
        }

        return $list;
    }

    /**
     * 清楚系统缓存
     * @param int $type 类型：1-删除缓存 2-删除日志 3-删除临时文件
     * @return bool
     */
    public static function clear($type)
    {
        $type = $type?$type:1;
        $sysClearService = (new SysClearService);

        switch ($type) {
            case 1:
                $sysClearService->clearCache();
                break;
            case 2:
                $sysClearService->deleteLog();
                break;
            case 3:
                $sysClearService->deleteTempFile();
                break;
            default:
        }

        return true;
    }
}